package com.dukk.espark.core;

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SQLContext;

import java.io.Serializable;

/**
 * 数据库基础操作类
 * @author shengshi_feiyang@yeah.net
 * @version 2020-08-22
 */
public abstract class BaseJdbcDao implements Serializable {
    private static final long serialVersionUID = 2329927023862136750L;

    protected SQLContext sqlContext;

    protected DBInfo dbInfo;


    public BaseJdbcDao(SQLContext sqlContext, DBInfo dbInfo){
        this.sqlContext = sqlContext;
        this.dbInfo = dbInfo;
    }

    /**
     *  更加sql读取数据库信息
     */
    protected  Dataset<Row> jdbcRead(String querySql){
        return this.sqlContext.read().format("jdbc")
                .option("url", this.dbInfo.getUrl())
                .option("query", querySql)
                .option("user", this.dbInfo.getDbUserName())
                .option("password", this.dbInfo.getDbPassword())
                .load();
    }
}
